'use strict';

const baseResultServer = require('./baseresult');
class UserService extends baseResultServer {
  async addCart(form) {
    const { ctx } = this;
    let result = '';
    let userId = form.userId;
    let productId = form.productId;
    const userDoc = await ctx.model.User.findOne({userId:userId});
    if (userDoc) {
          var goodsItem = '';
          /*判断id 是否重复*/
          userDoc.cartList.forEach(function(item) {
            if (item.productId == productId) {
              goodsItem = item;
              item.productNum++;
            }
          });
          if (goodsItem) {
           const userDocSave = await userDoc.save();
            result = this.isOperate(userDocSave)
          } else {
            const findGoods = await ctx.model.Goods.findOne({ productId: productId });
            if (findGoods.errors) {
              result = this.isOperate(findGoods)
            } else {
              if (findGoods) {
                findGoods.productNum = 1;
                findGoods.checked = 1;
                userDoc.cartList.push(findGoods);
                const userDocSave = await userDoc.save();
                result = this.isOperate(userDocSave)
              }
            }
          }
        }else{
          result = this.operateFunError();
        }
    return result;
  }
  async userLogin(form){
    const {ctx} = this;
    let userName = form.userName;
    let userPwd = form.userPwd;
    const userDoc = await ctx.model.User.findOne(form);
    return this.isLogin(userDoc)

  }
  async getUserInfor(form){
    return this.decryptFun()
  }
}
module.exports = UserService;
